#include <rtthread.h>
#include <rtdevice.h>
#include <string.h>
#include <stdlib.h>
#include <app_guide_srv.h>
#include <app_sys_param.h>
#include <app_charge_param.h>
#include <app_system_msg.h>
#include "app_ocpp_key_value.h"
#ifdef RT_USING_FINSH

static int charge(int argc, char **argv)
{
    int result = RT_EOK;
	uint8_t charge_ev_enable = 0;
	uint32_t charge_ev_time = 0;
	uint32_t charge_return_time = 0;
	if (argc != 4){
		
		if (!(strcmp(argv[1], "time"))){
			time_t time_now = time(RT_NULL);
			rt_kprintf("time_now is %d\n",time_now);
			return result;
		}
		if (!(strcmp(argv[1], "recover"))){
			app_system_restore_factory_msg_post();
			rt_kprintf("waiting recover------");
			return result;
		}
		if ((strcmp(argv[1], "reboot") != 0)){
			rt_kprintf("please enter charge ev/ev_time/return_time set/get num");
			result = -RT_ERROR;
		} else {
			app_system_reset_msg_post();
		}
		
		
	} else {
			
			if ((strcmp(argv[2], "set") != 0)){
				if ((strcmp(argv[2], "get") != 0)){
					rt_kprintf("please enter charge ev/ev_time/return_time set/get num");
					result = -RT_ERROR;
				}
				
			}
				
				if (!(strcmp(argv[2], "set"))){
					
					if (!strcmp(argv[1], "ev")) {
						
						charge_ev_enable = atoi(argv[3]);
						app_charge_param_set(SYS_CHARGE_EV_ENABLE, SYS_PARAM_TYPE_HEX, &charge_ev_enable);
						rt_kprintf("charge %s %s %s\n", argv[1], argv[2], "success");
						
					} else if (!strcmp(argv[1], "ev_time")){
					
						charge_ev_time = atoi(argv[3]);
						app_charge_param_set(SYS_CHARGE_EV_TIME, SYS_PARAM_TYPE_HEX, &charge_ev_time);
						rt_kprintf("charge %s %s %s\n", argv[1], argv[2], "success");
						
					}else if (!strcmp(argv[1], "return_time")){
					
						charge_return_time = atoi(argv[3]);
						app_sys_param_set(APP_SYS_PARAM_UI_RETURN, SYS_PARAM_TYPE_HEX, &charge_return_time);
						rt_kprintf("charge %s %s %s\n", argv[1], argv[2], "success");
						
					}else if (!strcmp(argv[1], "charge_mode")){
						uint8_t mode = atoi(argv[3]);
						app_charge_param_set(SYS_CHARGE_MODE,CHARGE_PARAM_TYPE_HEX,&mode);
					} else {
						rt_kprintf("please enter charge ev/ev_time/return_time set/get num");
						result = -RT_ERROR;
					}
					
				} else if (!(strcmp(argv[2], "get"))){
					
					if (!strcmp(argv[1], "ev")) {
						
						app_charge_param_get(SYS_CHARGE_EV_ENABLE, &charge_ev_enable, sizeof(charge_ev_enable));
						rt_kprintf("charge %s %s %d\n", argv[1], argv[2], charge_ev_enable);
						
					} else if (!strcmp(argv[1], "ev_time")){
					
						app_charge_param_get(SYS_CHARGE_EV_TIME, &charge_ev_time, sizeof(charge_ev_time));
						rt_kprintf("charge %s %s %d \n", argv[1], argv[2], charge_ev_time);
						
					}else if (!strcmp(argv[1], "return_time")){
					
						app_sys_param_get(APP_SYS_PARAM_UI_RETURN, &charge_return_time, sizeof(charge_return_time));
						rt_kprintf("charge %s %s %d\n", argv[1], argv[2], charge_return_time);
						
					} else {
						rt_kprintf("please enter charge ev/ev_time/return_time set/get num");
						result = -RT_ERROR;
					}
					
				}
				
			
		
	}
    return RT_EOK;
}
MSH_CMD_EXPORT(charge, charge function);
static int offline_set(int argc, char **argv)
{
    int result = RT_EOK;
	uint16_t offline_key = 15;
	uint16_t get_vlaue = 0;
	if (argc == 2){
		
		if (!(strcmp(argv[1], "set"))){
			offline_key = atoi(argv[2]);
			ocpp_key_set_value(E_OCPP_KEY_OM,&offline_key,sizeof(offline_key));			
			ocpp_key_get_value(E_OCPP_KEY_OM,&get_vlaue,sizeof(get_vlaue));
			rt_kprintf("set key = %d \n",get_vlaue);
			return result;
		}else
		if (!(strcmp(argv[1], "clear"))){
			ocpp_key_set_value(E_OCPP_KEY_OM,&offline_key,sizeof(offline_key));	
			return result;
		} else {
			rt_kprintf("input set or clear \n");
			result = -RT_ERROR;
		}
		
	} else {
		rt_kprintf("input set or clear \n");
		result = -RT_ERROR;
	}
    return result;
}
MSH_CMD_EXPORT(offline_set, offline_set function);



#ifdef DEBUG_CP_SET_ENABLE
static int cp_set(int argc, char **argv)
{
    int result = RT_EOK;
	uint8_t charge_ev_enable = 0;
	uint32_t charge_ev_time = 0;
	uint32_t charge_return_time = 0;
	if (argc == 2){
		
		if (!(strcmp(argv[1], "0v"))){
			debug_cp_set(3);
			return result;
		}else
		if (!(strcmp(argv[1], "6v"))){
			debug_cp_set(2);
			return result;
		}else
		if (!(strcmp(argv[1], "9v"))){
			debug_cp_set(1);
			return result;
		}else
		if (!(strcmp(argv[1], "12v"))){
			debug_cp_set(0);
			return result;
		} else {
			rt_kprintf("usage: cp_set 0v/6v/9v/12v\n");
			result = -RT_ERROR;
		}
		
	} else {
		rt_kprintf("usage: cp_set 0v/6v/9v/12v\n");
		result = -RT_ERROR;
	}
    return result;
}
MSH_CMD_EXPORT(cp_set, cp_set function);
#endif
#endif /* RT_USING_FINSH */